# https://code.google.com/p/unihan-52plus-sqlite-3-db/
# 
# This script merges the individual data file from Unihan releases, newer than
# 5.2.0, into the previously used one single monolithic file, e.g. Unihan.txt,
# that has been used until 5.1.0.
#
# The then-merged file could be processed by the uni_db.awk, originally from
# http://code.google.com/p/unihan-sqlite-3-database/ and modified by
# https://code.google.com/p/unihan-52plus-sqlite-3-db/, 
# to convert the CSV-like Unihan data file into Sqlite3 database
#


function usage(){
    usage_str = \
	"Usage: awk -f [this_awk_file] -v OUTFILE=[name_of_desired_merged_file] [all_Unihan_release_files]\n" \
	"e.g. awk -f merge_unihan_ram.awk -v OUTFILE=Unihan_merged.txt unihan_latest/*.txt"
    print usage_str
}

BEGIN{
    # check if OUTFILE is specified
    if(OUTFLIE==""){
	usage()
	exit(1)
    }
    split("",entries)
    split("",entry_num)
    
}

/^U\+/ {

    if( $1 in entry_num ){
	entry_num[$1] = entry_num[$1] + 1
    }else{
	entry_num[$1] = 1
    }

    entries[$1,entry_num[$1]] = $0
}

END{
    for (entry in entry_num){
	for(i=1;i<=entry_num[entry];i++){
 	    print entries[entry,i] >> OUTFILE
	}
    }
    close(OUTFILE)
    printf("Result file %s has been successfully created! Now run the uni_db_unix_awk to generate the sqlite3 db\n", OUTFILE)
}
